package com.lmaos.basetools.select;

import java.util.ArrayList;
import java.util.List;

public class SelectUtils {
	/** 选择
	 * 
	 * @param in 输入条件
	 * @param selects
	 * @return
	 */
	public static List<Select> select(double in,Select... selects){
		List<Select> result = new ArrayList<Select>();
		if(selects !=null){
			for (Select select : selects) {
				if(select.getMin()<=in&&select.getMax()>=in){
					result.add(select);
				}
			}
		}
		return result;
	}
	
	public static <T extends SelectBean>T selectOne(Class<T>... selectBeanClasses){
		return selectOne(null, selectBeanClasses);
	}
	public static <T extends SelectBean>List<SelectBean> select(Class<T>... selectBeanClasses){
		return select(null,selectBeanClasses);
	}
	public static <T extends SelectBean>T selectOne(Object obj,Class<T>... selectBeanClasses){
		try {
			SelectBean[] selectBeans = new SelectBean[selectBeanClasses.length];
			for (int i = 0; i < selectBeans.length; i++) {
					selectBeans[i] = selectBeanClasses[i].newInstance();
			}
			return selectOne(obj,selectBeans);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public static <T extends SelectBean> List<SelectBean> select(Object obj,Class<T>... selectBeanClasses){
		
		try {
			SelectBean[] selectBeans = new SelectBean[selectBeanClasses.length];
			for (int i = 0; i < selectBeans.length; i++) {
					selectBeans[i] = selectBeanClasses[i].newInstance();
			}
			return select(obj,selectBeans);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static <T extends SelectBean>T selectOne(SelectBean... selectBeans){
		return selectOne(null, selectBeans);
	}
	public static List<SelectBean> select(SelectBean... selectBeans){
		return select(null, selectBeans);
	}
	public static <T extends SelectBean>T selectOne(Object obj,SelectBean... selectBeans){
		for (SelectBean selectBean : selectBeans) {
			if(selectBean.isSelected(null)){
				return (T) selectBean;
			}
		}
		return null;
	}
	public static List<SelectBean> select(Object obj,SelectBean... selectBeans){
		List<SelectBean> selects = new ArrayList<SelectBean>();
		for (SelectBean selectBean : selectBeans) {
			if(selectBean.isSelected(null)){
				selects.add(selectBean);
			}
		}
		return selects;
	}
	
}
